﻿namespace JobButenko.Models.BusinessLogicLayer
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    public class ResumeRepository
    {
        private readonly JobDataContext dataContext;

        public ResumeRepository(JobDataContext dataContext)
        {
            this.dataContext = dataContext;
        }

        public void CreateResume(Resume resume)
        {
            var dataManager = new DataManager();
            int candidateId =
                dataManager.Candidate.GetCandidateFromUserName(HttpContext.Current.User.Identity.Name).CandidateId;

            var newResume = new Resume
                                {
                                    CurrProfFieldId = resume.CurrProfFieldId,
                                    DesProfFieldId = resume.DesProfFieldId,
                                    CurrPostId = resume.CurrPostId,
                                    DesPostId = resume.DesPostId,
                                    SalaryId = resume.SalaryId,
                                    StateResume = resume.StateResume,
                                    CandidateId = candidateId
                                };

            this.dataContext.Resumes.InsertOnSubmit(newResume);
            this.dataContext.SubmitChanges();
        }

        public IEnumerable<Resume> GetResumeFromUser(string userName)
        {
            return this.dataContext.Resumes.Where(resume => resume.Candidate.User.UserName.Equals(userName));
        }

        public Resume GetResumeFromId(int id)
        {
            return this.dataContext.Resumes.SingleOrDefault(resume => resume.ResumeId.Equals(id));
        }

        public void UpdateResume(int id, Resume resume)
        {
            var resumeDb = this.GetResumeFromId(id);

            resumeDb.CurrProfFieldId = resume.CurrProfFieldId;
            resumeDb.CurrPostId = resume.CurrPostId;
            resumeDb.DesProfFieldId = resume.DesProfFieldId;
            resumeDb.DesPostId = resume.DesPostId;
            resumeDb.SalaryId = resume.SalaryId;
            resumeDb.StateResume = resume.StateResume;

            this.dataContext.SubmitChanges();
        }

        public void DeleteResume(int id)
        {
            var resume = this.GetResumeFromId(id);

            this.dataContext.Resumes.DeleteOnSubmit(resume);
            this.dataContext.SubmitChanges();
        }

        public List<Resume> SearchResume(Resume resume)
        {
            var resultResume = this.dataContext.Resumes.Where(r => r.StateResume.Equals(true));
            if (resume.CurrProfFieldId != 1)
            {
                resultResume = resultResume.Where(r => r.CurrProfFieldId.Equals(resume.CurrProfFieldId));
            }

            if (resume.CurrPostId != 1)
            {
                resultResume = resultResume.Where(r => r.CurrPostId.Equals(resume.CurrPostId));
            }

            if (resume.DesProfFieldId != 1)
            {
                resultResume = resultResume.Where(r => r.DesProfFieldId.Equals(resume.DesProfFieldId));
            }

            if (resume.DesPostId != 1)
            {
                resultResume = resultResume.Where(r => r.DesPostId.Equals(resume.DesPostId));
            }

            if (resume.SalaryId != 1)
            {
                resultResume = resultResume.Where(r => r.SalaryId.Equals(resume.SalaryId));
            }

            return resultResume.ToList();
        }
    }
}